{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "for folder in [\n",
    "    Path(r\"C:\\Euclid_Jie\\barra\\submodule\\nav_analysis\\barra_data\\cne5\"),\n",
    "    Path(r\"C:\\Euclid_Jie\\barra\\submodule\\nav_analysis\\barra_data\\cne6\"),\n",
    "]:\n",
    "    merged_data = pd.DataFrame()\n",
    "    for file in folder.glob(\"*.xlsx\"):\n",
    "        barra_name = file.stem.split(\"-\")[0].replace(\"因子\", \"\")\n",
    "        tmp_data = pd.read_excel(file).iloc[:-1]\n",
    "        tmp_data.set_index(\"日期\", inplace=True)\n",
    "        tmp_data.columns = [f\"{barra_name}\"]\n",
    "        tmp_data[barra_name] = tmp_data[barra_name].apply(\n",
    "            lambda x: float(x.replace(\"%\", \"\")) / 100\n",
    "        )\n",
    "        merged_data = pd.concat([merged_data, tmp_data], axis=1)\n",
    "    merged_data.sort_index(inplace=True)\n",
    "    merged_data = merged_data.iloc[1:]\n",
    "    merged_data.to_csv(folder.joinpath(f\"merged_{folder.name}_barra_factor_rtn.csv\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
